package com.chain.qs.modules.biz.web;

import com.chain.qs.common.config.Global;
import com.chain.qs.common.persistence.Page;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.HandoverInfo;
import com.chain.qs.modules.biz.service.HandoverInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 值班设置Controller
 *
 * @author Wang zhiping
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/handoverInfo")
public class HandoverInfoController extends BaseController {

    @Resource
    private HandoverInfoService handoverInfoService;

    @ModelAttribute
    public HandoverInfo get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return handoverInfoService.get(id);
        } else {
            return new HandoverInfo();
        }
    }

    //    @RequiresPermissions("sys:handover:view")
    @RequestMapping(value = {"list", ""})
    public String list(HandoverInfo handoverInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<HandoverInfo> page = handoverInfoService.findPage(new Page<HandoverInfo>(request, response), handoverInfo);
        model.addAttribute("page", page);

        return "modules/biz/handoverInfoList";
    }

    //    @RequiresPermissions("sys:handover:view")
    @RequestMapping(value = "form")
    public String from(@RequestParam(required = false)String id, Model model) {
        HandoverInfo handoverInfo = handoverInfoService.get(id);
        model.addAttribute(handoverInfo);
        return "modules/biz/handoverInfoForm";
    }

    //    @ResponseBody
    @RequiresPermissions("sys:handover:edit")
    @RequestMapping(value = "save")
    @ResponseBody
    public String save(HandoverInfo handoverInfo, HttpServletRequest request) {

        if (handoverInfo == null) {
            return "failure";
        }

        Map result = handoverInfoService.save(handoverInfo, request);
        return result.toString().replaceAll("=", ":");

    }

    @RequestMapping(value = "checkTime")
    @ResponseBody
    public String checkTime() {
        return handoverInfoService.checkTime() + "";
    }

    @RequiresPermissions("sys:handover:edit")
    @RequestMapping(value = "delete")
    public String delete(HandoverInfo handoverInfo, RedirectAttributes redirectAttributes) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/biz/handoverInfo/";
        }
        handoverInfoService.delete(handoverInfo);
        addMessage(redirectAttributes, "删除排班成功");
        return "redirect:" + adminPath + "/biz/handoverInfo/";
    }

}
